package com.sl.plugin.step.db;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.sl.core.engine.datasource.DataSourceManager;
import com.sl.core.engine.meta.ProcessCompDescriptorService;
import com.sl.core.engine.meta.impl.AbstractProcessCompDescriptor;
import com.sl.core.engine.meta.row.ProcessRowDescriptor;
import com.sl.core.engine.meta.valuemeta.ValueMetaArray;
import com.sl.core.engine.meta.valuemeta.ValueMetaObject;
import com.sl.core.engine.utils.BeanUtil;
import lombok.Getter;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.value.ValueMetaBigNumber;

import java.util.Map;

/**
 *
 */
@Getter
public class DbQueryCompDescriptor extends AbstractProcessCompDescriptor<
        DbQueryComp, DbQueryCompContext> implements ProcessCompDescriptorService {


//    @Override
//    protected ProcessCompService getComp() {
//        return new DbQueryComp(this);
//    }
//
//    @Override
//    public ProcessCompContextService getContext() {
//        return new DbQueryCompContext();
//    }

    @Override
    public void loadDescriptor(Map<String, Object> map) {
        String dataSourceId = BeanUtil.getStr(map, "input.dataSourceId");
        String schemaId = BeanUtil.getStr(map, "input.schemaId");
        String tableName = BeanUtil.getStr(map, "input.tableName");
        String sql = BeanUtil.getStr(map, "input.sql");
        Boolean pageFlag = BeanUtil.getBool(map, "input.pageFlag");

        if (StrUtil.isBlank(dataSourceId)) {
            return;
        }
        if (StrUtil.isBlank(sql)) {
            return;
        }


        IRowMeta queryFields = DataSourceManager.INSTANCE.getQueryFields(dataSourceId, sql);
        ProcessRowDescriptor rowDescriptor = new ProcessRowDescriptor(queryFields);
        rowDescriptor.setIsArray(true);
        rowDescriptor.setBasicTypeFlag(false);

        if (BooleanUtil.isTrue(pageFlag)) {
            ProcessRowDescriptor processRowDescriptor = new ProcessRowDescriptor();
            processRowDescriptor.setIsArray(false);
            processRowDescriptor.setBasicTypeFlag(false);

            ValueMetaBigNumber bigNumber = new ValueMetaBigNumber();
            bigNumber.setName("total");
            bigNumber.setComments("总条数");

            ValueMetaBigNumber size = new ValueMetaBigNumber();
            size.setName("size");
            size.setComments("每页显示条数");

            ValueMetaBigNumber current = new ValueMetaBigNumber();
            current.setName("current");
            current.setComments("当前页");

            ValueMetaArray records = new ValueMetaArray();
            records.setName("records");
            records.setComments("记录");
            records.setFieldType(new ValueMetaObject());
            records.setFields(rowDescriptor.getValueMetaList());
//            processRowDescriptor.setBasicFieldType();
            processRowDescriptor.addValueMeta(bigNumber);
            processRowDescriptor.addValueMeta(size);
            processRowDescriptor.addValueMeta(current);
            processRowDescriptor.addValueMeta(records);
            rowDescriptor = processRowDescriptor;
        }

        setResultRowDescriptor(rowDescriptor);
    }


    @Override
    public ProcessRowDescriptor getResultDescriptor(ProcessRowDescriptor rowDescriptor) {
        ProcessRowDescriptor rowMeta = super.getResultRowDescriptor();
        return rowMeta;
    }

}
